package cn.itsource.sm.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageBean;
import cn.itsource.sm.domain.Systemdictionarydetail;
import cn.itsource.sm.domain.Systemdictionarytype;
import cn.itsource.sm.query.SystemdictionarytypeQuery;
import cn.itsource.sm.service.ISystemdictionarydetailService;
import cn.itsource.sm.service.ISystemdictionarytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sm")
public class SystemdictionarytypeController {
    @Autowired
    private ISystemdictionarytypeService service;
    @Autowired
    private ISystemdictionarydetailService iservice;

    @PutMapping
    public AjaxResult saveorupdate(@RequestBody Systemdictionarytype systemdictionarytype) {
        try {
            if (systemdictionarytype.getId() == null) {
                service.add(systemdictionarytype);
            } else {
                service.update(systemdictionarytype);
            }
        } catch (Exception e) {
            return new AjaxResult(false, "失败");
        }
        return new AjaxResult();
    }



    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            service.del(id);
            iservice.dele(id);
        } catch (Exception e) {
            return new AjaxResult(false, "操作失败");
        }
        return new AjaxResult();
    }

    @PostMapping
    public PageBean<Systemdictionarytype> fenye(@RequestBody SystemdictionarytypeQuery query) {
        return service.queryhigt(query);
    }

    @GetMapping("/find")
    public List<Systemdictionarytype> findall() {
        List<Systemdictionarytype> list=service.qaueryall();
        return service.qaueryall();
    }

    @GetMapping("/{id}")
    public Systemdictionarytype fingbuid(@PathVariable("id") Long id) {
        return service.querybyid(id);
    }

    @PostMapping("/del")
    public AjaxResult delList(@RequestBody List<Systemdictionarytype> systemdictionarytype){
        System.out.println("lao1");
        System.out.println(systemdictionarytype);
        try {
            for (Systemdictionarytype de:systemdictionarytype) {
                service.del(de.getId());

            }
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false,e.getMessage());
        }
    }
}
